<?php

namespace App\Http\Controllers\Vhome\Register;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
//引用对应的命名空间
use Gregwar\Captcha\CaptchaBuilder;
use Session;
class RegisterController extends Controller
{	
	//显示注册页面
    public function register()
	{	//显示注册页面
		return view('vhome/register');
	}
	

	//注册页面操作
	public function insert(Request $request){
		//验证码验证
		$userInput = \Request::get('captcha');

		if (Session::get('milkcaptcha') != $userInput) {
			//用户输入验证码错误
			return error('/vhome/register','您输入验证码错误',3);
		}
		
		
		//两次密码是否一致
		$pwd = $request->input('upwd');
		$repwd = $request->input('repwd');
		//var_dump($request);
		if($pwd==$repwd){
			echo '密码相同';
		}else{
			echo '密码不一致';
		}
		//查询用户字段
		$user=$request->only('uname');
		
		//查询用户数据
		$result1=DB::table('user')->where('uname','=',$user)->first();
		
		//判断用户名是否重复
		if($result1 != ''){
			return error('/vhome/register','用户名重复',3);
		}
		//查询邮箱字段
		$uemail=$request->only('uemail');
		//查询邮箱数据
		$result2=DB::table('user')->where('uemail','=',$uemail)->first();
		//判断邮箱是否重复
		if($result2 != ''){
			return error('/vhome/register','邮箱重复',3);
		}
		//获取插入信息
		$user=$request->only(['uname','utel','uemail','upwd']);
		//密码加密操作
		$user['upwd']=md5($user['upwd']);
		//将用户插入数据库
		$result=DB::table('user')->insert($user);
		//return $user;
		//判断用户是否插入成功
		if($result){
			return success('/vhome/login','注册成功请您登陆',1);
		}else{
			return error('/vhome/register/insert','注册失败',3);
		}
	}
	//生成验证码方法
	public function vcode()
	{
		//生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 120, $height = 60, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
	}
}
